package com.example.demo.metro.service.impl;

import com.example.demo.R;
import com.example.demo.metro.dtovo.FeedBackDTO;
import com.example.demo.metro.dtovo.FeedBackDetailVO;
import com.example.demo.metro.entity.FeedBack;
import com.example.demo.metro.repository.FeedBackRepository;
import com.example.demo.metro.service.FeedBackService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

/**
 * @author : yuanchuan
 * @date : 2023-11-22 20:42
 * @team : 西南综合开发支持部
 * @copyright :
 **/
@Transactional
@Service(value = FeedBackServiceImpl.BEAN_NAME)
public class FeedBackServiceImpl implements FeedBackService {
    public static final String BEAN_NAME = "feedBackService";

    @Autowired
    FeedBackRepository feedBackRepository;

    @Override
    public R save(FeedBackDTO data) {
        FeedBack feedBack = new FeedBack();
        BeanUtils.copyProperties(data, feedBack);
        feedBack.setUserId(1L);
        feedBack.setAppType("apple");
        feedBack.setCreateTime(new Date());
        feedBack.setId(new Date().getTime());
        feedBackRepository.save(feedBack);
        return R.ok();
    }

    @Override
    public R update(FeedBackDTO data) {
        FeedBack feedBack = feedBackRepository.findById(data.getId());
        feedBack.setContent(data.getContent());
        feedBack.setTitle(data.getTitle());
        feedBackRepository.save(feedBack);
        return R.ok();
    }

    @Override
    public FeedBackDetailVO getNoticeById(String id) {
        Optional<FeedBack> optional = feedBackRepository.findById(id);
        if(optional.isPresent()){
            FeedBackDetailVO dto = new FeedBackDetailVO();
            FeedBack feedBack = optional.get();
            BeanUtils.copyProperties(feedBack, dto);
            return dto;
        }
        return null;
    }

    @Override
    public void deleteByIds(String ids) {
        List<Long> list = new ArrayList<>();
        for(String id : ids.split(",")){
            list.add(Long.valueOf(id));
        }
        feedBackRepository.deleteByIdIn(list);
    }

    @Override
    public Page<FeedBack> findDataList(String title, int currentPage, int pageSize) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(currentPage - 1, pageSize, sort);
        Specification<FeedBack> authorityQuery=new Specification<FeedBack>() {
            @Override
            public Predicate toPredicate(Root<FeedBack> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                if(!StringUtils.isEmpty(title)) {
                    predicates.add(criteriaBuilder.like(root.get("title"), title));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page<FeedBack> dataList = this.feedBackRepository.findAll(authorityQuery, pageable);
        return dataList;
    }
}
